package com.jrtc.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jrtc.base.config.constants.PublicConstants;
import com.jrtc.base.entity.bo.*;
import com.jrtc.base.entity.vo.CatchBallRecordVo;
import com.jrtc.base.entity.vo.FingerFuckRecordVo;
import com.jrtc.base.entity.vo.IdNameInfoVo;
import com.jrtc.dao.*;
import com.jrtc.service.CatchBallRecordService;
import com.jrtc.service.FingerFuckRecordService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service("fingerFuckRecordService")
public class FingerFuckRecordServiceImpl implements FingerFuckRecordService {
    @Resource
    private FingerFuckRecordDao fingerFuckRecordDao;
    @Resource
    private PatientDao patientDao;
    @Resource
    private FingerFuckDetailedDao fingerFuckDetailedDao;
    @Resource
    private ExerciserecordDao exerciserecordDao;


    /**
     * 添加患者手指操记录
     * */
    @Override
    public FingerFuckRecordVo insertRecord(FingerFuckRecord fingerFuckRecord) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyy-MM-dd");
        FingerFuckRecordVo fingerFuckRecordVo = new FingerFuckRecordVo();
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, -7);//7天
        Date dBefore = calendar.getTime();
        calendar.add(Calendar.MONTH, -1);//1个月
        Date oneMoon = calendar.getTime();
        calendar.add(Calendar.MONTH, -3);//3个月
        Date threeMoon = calendar.getTime();

        Patient patient = patientDao.needBallCount(fingerFuckRecord.getIdCard());
        if (patient != null){
            fingerFuckRecord.setPatientId(patient.getPatientId());
            fingerFuckRecord.setCreationTime(date);
            fingerFuckRecord.setHeartInfo("{\"heart\":" + JSON.toJSONString(fingerFuckRecord.getHeart()) + "}");
            fingerFuckRecord.setBloodSaturationInfo("{\"bloodSaturation\":" + JSON.toJSONString(fingerFuckRecord.getBloodSaturation()) + "}");
            fingerFuckRecord.setHrvInfo("{\"hrv\":" + JSON.toJSONString(fingerFuckRecord.getHrv()) + "}");
            fingerFuckRecord.setBloodAreaInfo("{\"bloodArea\":" + JSON.toJSONString(fingerFuckRecord.getBloodArea()) + "}");
            int i = fingerFuckRecordDao.insertRecord(fingerFuckRecord);
            if (i <= 0){
                throw new Exception("添加患者手指操记录失败");
            }

            if (fingerFuckRecord.getFingerFuckDetailedList() != null && fingerFuckRecord.getFingerFuckDetailedList().size() > 0){
                for (FingerFuckDetailed fingerFuckDetailed : fingerFuckRecord.getFingerFuckDetailedList()) {
                    fingerFuckDetailed.setFingerFuckRecordId(fingerFuckRecord.getFingerFuckRecordId());
                    i = fingerFuckDetailedDao.insertRecord(fingerFuckDetailed);
                    if (i <= 0){
                        throw new Exception("添加患者手指操详情记录失败");
                    }
                }
            }

            Exerciserecord exerciserecord = new Exerciserecord();
            exerciserecord.setPatientId(patient.getPatientId());
            if (fingerFuckRecord.getMovementsNum() == fingerFuckRecord.getSuccessNum()){
                exerciserecord.setIsexercisefinish("true");
            }
            else {
                exerciserecord.setIsexercisefinish("false");
            }
            exerciserecord.setDuration(fingerFuckRecord.getFingerFuckRecordTime());
            exerciserecord.setDate(format.format(new Date()));
            exerciserecord.setActionType(PublicConstants.RecordEnum.FingerFuck.getValue());
            exerciserecord.setActionTypeId(fingerFuckRecord.getFingerFuckRecordId());
            exerciserecord.setPrescriptionName("手指操");
            int insert = exerciserecordDao.insertExerciserecord(exerciserecord);
            if (insert <= 0){
                throw new Exception("添加患者运动总记录失败");
            }

            List<FingerFuckRecord> fingerFuckRecords = fingerFuckRecordDao.selFingerFuckRecord(new Date(), dBefore, patient.getPatientId());
            if (fingerFuckRecords != null && fingerFuckRecords.size() > 0){
                for (FingerFuckRecord fuckRecord : fingerFuckRecords) {
                    List<FingerFuckDetailed> fingerFuckDetailedList = fingerFuckDetailedDao.selFingerFuckDetailed(fuckRecord.getFingerFuckRecordId());
                    if (fingerFuckDetailedList != null && fingerFuckDetailedList.size() > 0){
                        fuckRecord.setFingerFuckDetailedList(fingerFuckDetailedList);
                    }
                }
            }
            fingerFuckRecordVo.setSevenDay(fingerFuckRecords);

            List<FingerFuckRecord> fingerFuckRecordList = fingerFuckRecordDao.selFingerFuckRecord(new Date(), oneMoon, patient.getPatientId());
            if (fingerFuckRecordList != null && fingerFuckRecordList.size() > 0){
                for (FingerFuckRecord fuckRecord : fingerFuckRecordList) {
                    List<FingerFuckDetailed> fingerFuckDetailedList = fingerFuckDetailedDao.selFingerFuckDetailed(fuckRecord.getFingerFuckRecordId());
                    if (fingerFuckDetailedList != null && fingerFuckDetailedList.size() > 0){
                        fuckRecord.setFingerFuckDetailedList(fingerFuckDetailedList);
                    }
                }
            }
            fingerFuckRecordVo.setOneMoon(fingerFuckRecordList);

            List<FingerFuckRecord> moon = fingerFuckRecordDao.selFingerFuckRecord(new Date(), threeMoon, patient.getPatientId());
            if (moon != null && moon.size() > 0){
                for (FingerFuckRecord fuckRecord : moon) {
                    List<FingerFuckDetailed> fingerFuckDetailedList = fingerFuckDetailedDao.selFingerFuckDetailed(fuckRecord.getFingerFuckRecordId());
                    if (fingerFuckDetailedList != null && fingerFuckDetailedList.size() > 0){
                        fuckRecord.setFingerFuckDetailedList(fingerFuckDetailedList);
                    }
                }
            }
            fingerFuckRecordVo.setThreeMoon(moon);

            return fingerFuckRecordVo;
        }
        else {
            throw new Exception("证件号错误");
        }
    }

    /**
     * 手指操记录图表
     * */
    @Override
    public FingerFuckRecord record(Integer id) {
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, -7);//7天
        Date dBefore = calendar.getTime();
        calendar.add(Calendar.MONTH, -1);//1个月
        Date oneMoon = calendar.getTime();
        calendar.add(Calendar.MONTH, -3);//3个月
        Date threeMoon = calendar.getTime();
        Date endTime= new Date(date.getTime()+1000 * 60 * 60 * 24*1);

        FingerFuckRecord fingerFuckRecord = fingerFuckRecordDao.fingerFuckRecordById(id);

        List<FingerFuckRecord> fingerFuckRecordList = fingerFuckRecordDao.selFingerFuckRecord(endTime, dBefore, fingerFuckRecord.getPatientId());
        if (fingerFuckRecordList != null && fingerFuckRecordList.size() > 0){
            for (FingerFuckRecord fuckRecord : fingerFuckRecordList) {
                List<FingerFuckDetailed> fingerFuckDetailedList = fingerFuckDetailedDao.selFingerFuckDetailed(fuckRecord.getFingerFuckRecordId());
                if (fingerFuckDetailedList != null && fingerFuckDetailedList.size() > 0){
                    fuckRecord.setFingerFuckDetailedList(fingerFuckDetailedList);
                }
            }
        }
        fingerFuckRecord.setSevenDay(fingerFuckRecordList);

        List<FingerFuckRecord> fingerFuckRecords = fingerFuckRecordDao.selFingerFuckRecord(endTime, oneMoon, fingerFuckRecord.getPatientId());
        if (fingerFuckRecords != null && fingerFuckRecords.size() > 0){
            for (FingerFuckRecord fuckRecord : fingerFuckRecords) {
                List<FingerFuckDetailed> fingerFuckDetailedList = fingerFuckDetailedDao.selFingerFuckDetailed(fuckRecord.getFingerFuckRecordId());
                if (fingerFuckDetailedList != null && fingerFuckDetailedList.size() > 0){
                    fuckRecord.setFingerFuckDetailedList(fingerFuckDetailedList);
                }
            }
        }
        fingerFuckRecord.setOneMoon(fingerFuckRecords);

        List<FingerFuckRecord> three = fingerFuckRecordDao.selFingerFuckRecord(endTime, threeMoon, fingerFuckRecord.getPatientId());
        if (three != null && three.size() > 0){
            for (FingerFuckRecord fuckRecord : three) {
                List<FingerFuckDetailed> fingerFuckDetailedList = fingerFuckDetailedDao.selFingerFuckDetailed(fuckRecord.getFingerFuckRecordId());
                if (fingerFuckDetailedList != null && fingerFuckDetailedList.size() > 0){
                    fuckRecord.setFingerFuckDetailedList(fingerFuckDetailedList);
                }
            }
        }
        fingerFuckRecord.setThreeMoon(three);

        JSONObject jsonHeart = JSONObject.parseObject(fingerFuckRecord.getHeartInfo());
        String versionHeart = jsonHeart.getString("heart");
        List<IdNameInfoVo> heartList = JSON.parseArray(versionHeart, IdNameInfoVo.class);
        if (heartList != null && heartList.size() > 0){
            heartList.sort((x, y) -> Double.compare(x.getValue(), y.getValue()));
            for (IdNameInfoVo idNameInfoVo : heartList) {
                idNameInfoVo.setHighestValue(heartList.get(heartList.size() - 1).getValue());
                idNameInfoVo.setLowestValue(heartList.get(0).getValue());
            }
        }
        fingerFuckRecord.setHeart(heartList);

        JSONObject jsonBloodSaturation = JSONObject.parseObject(fingerFuckRecord.getBloodSaturationInfo());
        String versionBloodSaturation = jsonBloodSaturation.getString("bloodSaturation");
        List<IdNameInfoVo> bloodSaturationList = JSON.parseArray(versionBloodSaturation, IdNameInfoVo.class);
        if (bloodSaturationList != null && bloodSaturationList.size() > 0){
            bloodSaturationList.sort((x, y) -> Double.compare(x.getValue(), y.getValue()));
            for (IdNameInfoVo idNameInfoVo : bloodSaturationList) {
                idNameInfoVo.setHighestValue(bloodSaturationList.get(bloodSaturationList.size() - 1).getValue());
                idNameInfoVo.setLowestValue(bloodSaturationList.get(0).getValue());
            }
        }
        fingerFuckRecord.setBloodSaturation(bloodSaturationList);

        JSONObject jsonHrv = JSONObject.parseObject(fingerFuckRecord.getHrvInfo());
        String versionHrv = jsonHrv.getString("hrv");
        List<IdNameInfoVo> hrvList = JSON.parseArray(versionHrv, IdNameInfoVo.class);
        if (hrvList != null && hrvList.size() > 0){
            hrvList.sort((x, y) -> Double.compare(x.getValue(), y.getValue()));
            for (IdNameInfoVo idNameInfoVo : hrvList) {
                idNameInfoVo.setHighestValue(hrvList.get(hrvList.size() - 1).getValue());
                idNameInfoVo.setLowestValue(hrvList.get(0).getValue());
            }
        }
        fingerFuckRecord.setHrv(hrvList);

        JSONObject jsonBloodArea = JSONObject.parseObject(fingerFuckRecord.getBloodAreaInfo());
        String versionBloodArea = jsonBloodArea.getString("bloodArea");
        List<IdNameInfoVo> bloodAreaList = JSON.parseArray(versionBloodArea, IdNameInfoVo.class);
        if (bloodAreaList != null && bloodAreaList.size() > 0){
            bloodAreaList.sort((x, y) -> Double.compare(x.getValue(), y.getValue()));
            for (IdNameInfoVo idNameInfoVo : bloodAreaList) {
                idNameInfoVo.setHighestValue(bloodAreaList.get(bloodAreaList.size() - 1).getValue());
                idNameInfoVo.setLowestValue(bloodAreaList.get(0).getValue());
            }
        }
        fingerFuckRecord.setBloodArea(bloodAreaList);

        return fingerFuckRecord;
    }
}